﻿using System;
using System.Web;
using System.Web.Mvc;
using Hasiu.Entity;
using Hasiu.Service;
using Hasiu.Mvc.Filters;
using Hasiu.Mvc.Infrastructure;

namespace Hasiu.Mvc.Controllers.Admin
{
    public class HomeAdminController : Controller
    {
        [AdminAuthorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminInfo adminInfo, string cc)
        {
            
            if (string.IsNullOrEmpty(cc) || adminInfo == null)
            {
                return new RedirectOfOtherResult("请输入登陆信息", "/admin/login.aspx");
            }
            var checkcode = Request.Cookies["checkcode"].Value;
            if (!checkcode.Decrypt().ToLower().Equals(cc.ToLower()))
            {
                return new RedirectOfOtherResult("验证码错误", "/admin/login.aspx");
            }
            var adminInfo2 = ConfigService._.GetAdminInfo();
            if (adminInfo2.UserName != adminInfo.UserName || adminInfo2.Password != adminInfo.Password)
            {
                return new RedirectOfOtherResult("登陆失败", "/admin/login.aspx");
            }
            Response.Cookies.Add(new HttpCookie("admin", (DateTime.Now.AddDays(1) + "|" + adminInfo.UserName + "|" + adminInfo.Password).Encrypt()));
            return new RedirectResult("/admin.aspx");
        }

        public ActionResult LogOut()
        {
            UtilityHelper.EmptyCookies("admin");
            return new RedirectResult("/admin/login.aspx");
        }

        public ActionResult UpdatePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UpdatePassword(AdminInfo adminInfo1,AdminInfo adminInfo2)
        {
            var adminInfo = ConfigService._.GetAdminInfo();
            if (adminInfo.UserName != adminInfo1.UserName || adminInfo.Password != adminInfo1.Password)
            {
                return new RedirectOfOtherResult("原口令错误", "/admin/updatepassword.aspx");
            }
            ConfigService._.UpdateAdminInfo(adminInfo2);
            Response.Cookies.Add(new HttpCookie("admin", (DateTime.Now.AddDays(1) + "|" + adminInfo2.UserName + "|" + adminInfo2.Password).Encrypt()));
            return new RedirectOfUpdateResult("/admin/updatepassword.aspx");
        }
    }
}
